package com.example.mynetwork2.recyclerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mynetwork2.R;


import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private Context mContext;
    private List<NewsBean> datas;
    private LayoutInflater inflater;
    private OnItemClickListener itemClickListener;
    public MyAdapter(Context context, List<NewsBean> list){
        inflater = LayoutInflater.from(context);
        this.mContext=context;
        this.datas=list;
    }

    public void setDatas(List<NewsBean> datas){
        this.datas=datas;
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener){
        this.itemClickListener=itemClickListener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_layout, parent, false);
        MyViewHolder holder = new MyViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.title.setText(datas.get(position).getTitle());
        holder.content.setText(datas.get(position).getContent());
        Glide.with(mContext).load(datas.get(position).getUrl()).into(holder.imageView);

    }

    @Override
    public int getItemCount() {
        return this.datas.size();
    }

    public  class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView content;
        private ImageView imageView;
        private View rootView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.item_imageview);
            title=itemView.findViewById(R.id.title_tv);
            content=itemView.findViewById(R.id.content_tv);
            rootView=itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemClick(View view, NewsBean newsBean, int position);
        void onItemLongClick(View view, int position);
    }
}
